/**
 * 系统变量服务
 */

export interface SystemVariable {
  name: string;
  display_name: string;
  description: string;
  sample_value: string;
  field_type: string;
  is_system: boolean;
}

/**
 * 获取系统变量列表
 */
export function getSystemVariables(): SystemVariable[] {
  const now = new Date();
  
  return [
    {
      name: 'current_time',
      display_name: '当前时间',
      description: '当前完整时间（年-月-日 时:分:秒）',
      sample_value: now.toLocaleString('zh-CN', { 
        year: 'numeric', 
        month: '2-digit', 
        day: '2-digit', 
        hour: '2-digit', 
        minute: '2-digit', 
        second: '2-digit' 
      }),
      field_type: 'datetime',
      is_system: true,
    },
    {
      name: 'current_date',
      display_name: '当前日期',
      description: '当前日期（年-月-日）',
      sample_value: now.toLocaleDateString('zh-CN'),
      field_type: 'date',
      is_system: true,
    },
    {
      name: 'current_date_slash',
      display_name: '当前日期（斜杠格式）',
      description: '当前日期（年/月/日）',
      sample_value: `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')}`,
      field_type: 'date',
      is_system: true,
    },
    {
      name: 'current_date_dash',
      display_name: '当前日期（横杠格式）',
      description: '当前日期（年-月-日）',
      sample_value: `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`,
      field_type: 'date',
      is_system: true,
    },
    {
      name: 'current_date_compact',
      display_name: '当前日期（紧凑格式）',
      description: '当前日期（年月日）',
      sample_value: `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`,
      field_type: 'date',
      is_system: true,
    },
    {
      name: 'current_year_month',
      display_name: '当前年月',
      description: '当前年月（年月）',
      sample_value: `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`,
      field_type: 'date',
      is_system: true,
    },
    {
      name: 'current_month',
      display_name: '当前月份',
      description: '当前月份（月）',
      sample_value: String(now.getMonth() + 1).padStart(2, '0'),
      field_type: 'number',
      is_system: true,
    },
    {
      name: 'current_year',
      display_name: '当前年份',
      description: '当前年份（年）',
      sample_value: String(now.getFullYear()),
      field_type: 'number',
      is_system: true,
    },
    {
      name: 'current_day',
      display_name: '当前日',
      description: '当前日（日）',
      sample_value: String(now.getDate()).padStart(2, '0'),
      field_type: 'number',
      is_system: true,
    },
    {
      name: 'current_weekday',
      display_name: '当前星期',
      description: '当前星期几',
      sample_value: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][now.getDay()],
      field_type: 'string',
      is_system: true,
    },
  ];
}

/**
 * 根据变量名获取当前值
 */
export function getSystemVariableValue(variableName: string): string {
  const now = new Date();
  
  switch (variableName) {
    case 'current_time':
      return now.toLocaleString('zh-CN', { 
        year: 'numeric', 
        month: '2-digit', 
        day: '2-digit', 
        hour: '2-digit', 
        minute: '2-digit', 
        second: '2-digit' 
      });
    
    case 'current_date':
      return now.toLocaleDateString('zh-CN');
    
    case 'current_date_slash':
      return `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')}`;
    
    case 'current_date_dash':
      return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    
    case 'current_date_compact':
      return `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
    
    case 'current_year_month':
      return `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}`;
    
    case 'current_month':
      return String(now.getMonth() + 1).padStart(2, '0');
    
    case 'current_year':
      return String(now.getFullYear());
    
    case 'current_day':
      return String(now.getDate()).padStart(2, '0');
    
    case 'current_weekday':
      return ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][now.getDay()];
    
    default:
      return '';
  }
}
